// pages/home/components/chachengCom/chaSearch/chaSearch.js
const app = getApp();
app.func.page({

  /**
   * 组件的初始数据
   */
  data: {

  },
  onLoad: function (options) {
    console.log(options)
    wx.setNavigationBarTitle({
      title:options.name
    });
   this.setData({
     name:options.name
   })
    this.categories(options.name);
  },
 gosearch(name){
    app.func.postPromise('/product/search/0?length=1000',{ name:name })
    .then(([code,res])=>{
      console.log(res)
        this.setData({
          productlist:res.data,
        });
    })
 },

 categories: function (name) {
   let that =this;
  app.func.getPromise('/categories')
      .then(([code, res]) => {
        console.log(res)
          if (code == 200) {
            let count=0;
            res.data.forEach(v=>{
              if(name == v.name){
              that.ordershop(v.id);
              }else{
                count= count+1;
              }
            })
            if(count ==res.data.length){
              this.checkDataLength(0);
            }
          }
      });
},
ordershop:function(id){
  app.func.postPromise('/categorySku', {id:id,
    offset:0,
    type:0,
    key: '',
    min:0,
    max:10000,
    min_rate: '',
    max_rate:'',
    sort: '',
    length:100000,
    sup_id:'',
    brand: '',
  }).then(res=>{
    console.log(res)
    if(res[1].data.length==0){
      this.checkDataLength(0);
    }else{
      this.setData({
        shopList:res[1].data
      })
    }
  })
},
productdetail: function (e) {
  var id = e.currentTarget.dataset.id;
  wx.navigateTo({
    url: '/pages/index/productdetail/productdetail?id=' + id
  })
},
addcart:function(e){
  let id = e.currentTarget.dataset.id;
      app.func.postPromise('/addcartv2',
              {
                  pro_id:id,
                  count:1
              })  .then(([code, res]) => {
                  console.log(res);
                  if(code == 200){
                      wx.showToast({
                        title: '加入成功',
                        icon:'success',
                        mask:true,
                        duration:1000

                      })
                  }else{
                      wx.showToast({
                        title:res.message,
                        icon:'none',
                        mask:true
                      })
                  }
              })
},
checkDataLength(value){
  if(value ===0){
      app.func.confirm('提示','抱歉，没有找到相关商品', false)
          .then(()=>{
             wx.navigateBack({
                 delta: 1
             });
          })
  }
},
})
